<?php
/**
 * 交流部分
 * 
 * @package ews
 * @ews Enterprise WorkShop
 * @version 1.0 alpha
 * @since 2011-05-03
 * @author monkee
 */

class forumApp extends app
{
	public function index(){
		$forumobj = & $this->getModel('forum');
		return array(
			'data' => $forumobj->get_list()
		);
	}
	public function search(){
		$str = $this->getData('get/str');
		if(empty($str)){
			$this->message('内容为空');
		}
		$forumobj = & $this->getModel('forum');
		$list = $forumobj->search($str);
		if(empty($list)){
			$list = $forumobj->mulit_search($str);
			$new = true;
		}else{
			$new = false;
		}
		return array(
			'posts' => $list,
			'isnew' => $new,
			'subject' => $str 
		);
	}
	
	public function add(){
		$act = $this->getData('post/act');
		if(empty($act)){
			$data = $this->getData('post/subject');
			Cookie::set('forumid', 0);
			Cookie::set('forumsubject', $data);
			return array(
				'subject' => $data,
			);
		}
		$id = Cookie::get('forumid');
		$message = $this->getData('post/message');
		$subject = $this->getData('post/subject');
		$forumobj = & $this->getModel('forum');
		if($id == 0){
			$id = $forumobj->create($subject, $this->user_id, $message);
		}else{
			$forumobj->update($id, $message, $this->user_id);
		}
		$feedobj = & $this->getModel('feed');
		if(!empty($message)){
			if(empty($subject)){
				$message = '更新了内容：<a href="javascript:body_load(\'/forum/view\',{forumid:'.$id.'});">'.$message.'</a>';
			}else{
				$message = '发布了内容：<a href="javascript:body_load(\'/forum/view\',{forumid:'.$id.'});">'.$subject.'</a>, 更新了内容：<a href="javascript:body_load(\'/forum/view\',{forumid:'.$id.'});">'. $message.'</a>';
			}
		}
		$feedobj->publish($this->user_id, $this->user['username'], 2, $message, $id);
		Cookie::set('forumid', 0);
		return array('act' => 'succ');
	}
	
	public function attachment(){
		$filename = $this->getData('get/qqfile');
		if(empty($filename)){
			return array();
		}else{
			$file = file_get_contents("php://input");
			if(empty($file)){
				return array('error' => '文件上传失败');
			}
			$file_size = strlen($file);
			$forumobj = & $this->getModel('forum');
			$id = Cookie::get('forumid');
			$id = intval($id);
			if($id == 0){
				$subject = Cookie::get('forumsubject');
				if(empty($subject)){
					return array('error' => '文件过期，请重新更新');
				}
				$id = $forumobj->create($subject, $this->user_id, '', 1);
			}
			Cookie::set('forumid', $id);
			$attobj = &$this->getModel('attachment');
			$file_path = $attobj->save_file($file, $filename);
			if($file_path){
				$attobj->create($id, $this->user_id, $file_size, $filename, $file_path);
			}
			$feedobj = & $this->getModel('feed');
			if(empty($subject)){
				$message = '上传了新文件：<a href="javascript:body_load(\'/forum/view\',{forumid:'.$id.'});">'.$filename.'</a>';
			}else{
				$message = '发布了内容：<a href="javascript:body_load(\'/forum/view\',{forumid:'.$id.'});">'.$subject.'</a>, 并上传了文件：<a href="javascript:body_load(\'/forum/view\',{forumid:'.$id.'});">'. $filename .'</a>';
			}
			$feedobj->publish($this->user_id, $this->user['username'], 2, $message, $id);
			return array('success' => true);
		}
	}
	
	public function view(){
		$forum_id = $this->getData('post/forumid');
		$forum_id = intval(($forum_id));
		$forumobj = & $this->getModel('forum');
		$forum = $forumobj->get($forum_id);
		$posts = $forumobj->get_posts($forum_id);
		
		if(empty($forum)){
			$this->message('内容已经不存在');
		}
		Cookie::set('forumid', $forum_id);
		$attobj = & $this->getModel('attachment');
		$attachment = $attobj->get_attachments_by_forum_id($forum_id);
		
		$replyobj = & $this->getModel('reply');
		return array(
			'forum' => $forum,
			'attachments' => $attachment,
			'posts' => $posts,
			'reply' => $replyobj->get_replys(2, $forum_id),
		); 
	}
}
?>